using System;
using System.Collections;
using System.Collections.Generic;
using Rilisoft.MiniJson;
using UnityEngine;

public sealed class EventX3Manager : MonoBehaviour
{
	public delegate void Action();

	private const float EventX3UpdateTimeout = 900f;

	public static EventX3Manager Instance;

	public List<Action> promoList = new List<Action>();

	private bool _isGetEventX3InfoRunning;

	private float _eventX3GetInfoStartTime;

	private float _eventX3LastCheckTime;

	private long _eventX3StartTime;

	private long _eventX3Duration;

	private bool _eventX3Active;

	public bool IsEventX3Active
	{
		get
		{
			return _eventX3Active;
		}
	}

	public long EventX3RemainedTime
	{
		get
		{
			if (IsEventX3Active)
			{
				return _eventX3StartTime + _eventX3Duration - CurrentUnixTime;
			}
			return 0L;
		}
	}

	public static long CurrentUnixTime
	{
		get
		{
			DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
			return (long)(DateTime.UtcNow - dateTime).TotalSeconds;
		}
	}

	public static event Action EventX3Updated;

	private void Start()
	{
		if (Instance != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		Instance = this;
		_eventX3Active = true;
	}

	public void ShowPromo()
	{
		BCWDebug.LogWarning("ShowPromo", "x3AndBonus");
		X3AndBonusInMenu.instance.StartCoroutine(X3AndBonusInMenu.instance.ShowPromo());
	}

	public void ShowX3()
	{
		BCWDebug.LogWarning("ShowX3", "x3AndBonus");
		X3AndBonusInMenu.instance.StartCoroutine(X3AndBonusInMenu.instance.ShowX3());
	}

	public void ShowRateUs()
	{
		BCWDebug.LogWarning("ShowRateUs", "x3AndBonus");
		X3AndBonusInMenu.instance.StartCoroutine(controllerMenu.thisScript.ShowRateUs());
	}

	private void OnApplicationPause(bool pause)
	{
	}

	private IEnumerator GetEventX3InfoLoop()
	{
		while (true)
		{
			yield return StartCoroutine(GetEventX3Info());
			while (Time.realtimeSinceStartup - _eventX3GetInfoStartTime < 900f)
			{
				yield return null;
			}
		}
	}

	private IEnumerator GetEventX3Info()
	{
		if (_isGetEventX3InfoRunning)
		{
			yield break;
		}
		_eventX3GetInfoStartTime = Time.realtimeSinceStartup;
		_isGetEventX3InfoRunning = true;
		if (string.IsNullOrEmpty(URLs.EventX3))
		{
			_isGetEventX3InfoRunning = false;
			yield break;
		}
		WWW response = new WWW(URLs.EventX3);
		yield return response;
		BCWDebug.Log("Response: " + URLs.EventX3);
		if (!string.IsNullOrEmpty(response.error))
		{
			BCWDebug.LogWarning("EventX3 response error: " + response.error);
			_isGetEventX3InfoRunning = false;
			yield break;
		}
		if (string.IsNullOrEmpty(response.text))
		{
			BCWDebug.LogWarning("EventX3 response is empty");
			_isGetEventX3InfoRunning = false;
			yield break;
		}
		object eventX3InfoObj = Json.Deserialize(response.text);
		Dictionary<string, object> eventX3Info = eventX3InfoObj as Dictionary<string, object>;
		if (eventX3Info == null || !eventX3Info.ContainsKey("start") || !eventX3Info.ContainsKey("duration"))
		{
			BCWDebug.LogWarning("EventX3 response is bad");
			_isGetEventX3InfoRunning = false;
			yield break;
		}
		long startTime = (long)eventX3Info["start"];
		long duration = (long)eventX3Info["duration"];
		BCWDebug.Log(string.Format("EventX3: ({0}, {1})", startTime, duration));
		_eventX3StartTime = startTime;
		_eventX3Duration = duration;
		CheckEventX3Active();
		_isGetEventX3InfoRunning = false;
	}

	private void CheckEventX3Active()
	{
		bool eventX3Active = _eventX3Active;
		if (_eventX3StartTime != 0L && _eventX3Duration != 0L)
		{
			_eventX3Active = _eventX3StartTime < CurrentUnixTime && CurrentUnixTime < _eventX3StartTime + _eventX3Duration;
		}
		else
		{
			_eventX3StartTime = 0L;
			_eventX3Duration = 0L;
			_eventX3Active = false;
		}
		if (_eventX3Active != eventX3Active && EventX3Manager.EventX3Updated != null)
		{
			EventX3Manager.EventX3Updated();
		}
	}
}
